package com.zdemo.threadfactory;

import java.util.concurrent.atomic.AtomicInteger;
import java.util.logging.Level;
import java.util.logging.Logger;

public class MyAppThread extends Thread {

	public static final String DEFAULT_NAME = "default_name";

	private static volatile boolean debugLifecycle = false;
	private static final AtomicInteger created = new AtomicInteger();
	private static final AtomicInteger alive = new AtomicInteger();
	private static final Logger log = Logger.getAnonymousLogger();

	public MyAppThread(Runnable r) {
		this(r, DEFAULT_NAME);
	}

	public MyAppThread(Runnable r, String name) {
		super(r, name + "-" + created.incrementAndGet());
		setUncaughtExceptionHandler(new Thread.UncaughtExceptionHandler() {
			@Override
			public void uncaughtException(Thread t, Throwable e) {
				log.log(Level.SEVERE, "uncaught in thread " + t.getName());
			}
		});
	}

	public void run() {
		boolean debug = debugLifecycle;
		if (debug) {
			log.log(Level.FINE, "Created " + getName());
		}
		try {
			alive.incrementAndGet();
			super.run();
		} finally {
			alive.decrementAndGet();
			if(debug) {
				log.log(Level.FINE, "Exiting " + getName());
			}
		}
	}

	public static int getThreadsCreated() {
		return created.get();
	}

	public int getThreadAlive() {
		return alive.get();
	}

	public boolean getDebug() {
		return debugLifecycle;
	}

	public static void setDebug(boolean b) {
		debugLifecycle = b;
	}
}
